package com.MTR;

import java.io.*;

// write data into a csv file
public class CSVWriter {

	// Singleton pattern
	private static CSVWriter inst = null;
	private CSVWriter()
	{
	}
	public static CSVWriter instance()
	{
		if (inst == null)
		{
			inst = new CSVWriter();
		}
		return inst;
	}
	// end singleton
	
	FileOutputStream out;
    PrintStream p;
	
	public void open(String fileName, boolean append)
	{
		try {
			out = new FileOutputStream(fileName, append);
			p = new PrintStream(out);
		} catch (Exception e) 
		{
			e.printStackTrace();
		}
		
		p.println("Vehicle_Id, Node_Id, Temperature, Sample_time, Download_time");
	}
	
	public void close()
	{
		try {
		p.close();
		out.close();
		} catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("deprecation")
	public void append(MoteData d)
	{
		p.printf("%d, %d, %f, %s, %s", d.vehicleID, d.nodeID, d.temperature, d.sampleDate.toLocaleString(), d.downloadDate.toLocaleString());
		p.println();
	}

}
